<script setup>
import { ref, onMounted } from 'vue'
import { saveGoodsType } from '@/api/goods'
import { ElMessage } from 'element-plus'
import logger from '@/utils/logger'

// 组件属性
const props = defineProps({
  initialData: {
    type: Object,
    default: null,
  },
})

// 触发事件
const emit = defineEmits(['success', 'close'])

// 表单引用与数据
const formRef = ref(null)
const formData = ref({
  dealerGoodsTypeId: '',
  dealerGoodsTypeCode: '',
  dealerGoodsTypeName: '',
  dealerGoodsTypePId: -1,
})

// 表单校验规则
const rules = {
  dealerGoodsTypeCode: [{ required: true, message: '请输入类别编码', trigger: 'blur' }],
  dealerGoodsTypeName: [{ required: true, message: '请输入类别名称', trigger: 'blur' }],
}

const loading = ref(false)

// 初始化数据
onMounted(() => {
  // 如果是编辑模式，填充表单数据
  if (props.initialData) {
    formData.value = {
      dealerGoodsTypeId: props.initialData.dealerGoodsTypeId || '',
      dealerGoodsTypeCode: props.initialData.dealerGoodsTypeCode || '',
      dealerGoodsTypeName: props.initialData.dealerGoodsTypeName || '',
      dealerGoodsTypePId: props.initialData.dealerGoodsTypePId ?? -1,
    }
  } else {
    formData.value = {
      dealerGoodsTypeId: '',
      dealerGoodsTypeCode: '',
      dealerGoodsTypeName: '',
      dealerGoodsTypePId: -1,
    }
  }
})

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 后端已移除商品大类，默认使用 -1
        if (formData.value.dealerGoodsTypePId == null) {
          formData.value.dealerGoodsTypePId = -1
        }
        const res = await saveGoodsType(formData.value)
        if (res.success) {
          ElMessage.success(props.initialData ? '修改成功' : '添加成功')
          emit('success')
          emit('close')
        } else {
          ElMessage.error(res.msg || '操作失败')
        }
      } catch (error) {
        logger.error('保存商品类别失败', error)
        ElMessage.error('操作失败')
      } finally {
        loading.value = false
      }
    }
  })
}

</script>

<template>
  <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
    <el-form-item label="类别编码" prop="dealerGoodsTypeCode">
      <el-input v-model="formData.dealerGoodsTypeCode" placeholder="请输入类别编码" />
    </el-form-item>

    <el-form-item label="类别名称" prop="dealerGoodsTypeName">
      <el-input v-model="formData.dealerGoodsTypeName" placeholder="请输入类别名称" />
    </el-form-item>
  </el-form>
  <div class="dialog-footer">
    <el-button @click="emit('close')">
      <template #icon>
        <el-icon><LineMdClose /></el-icon>
      </template>
      取消
    </el-button>
    <el-button type="primary" :loading="loading" @click="handleSubmit">
      <template #icon>
        <el-icon><LineMdPlus /></el-icon>
      </template>
      保存
    </el-button>
  </div>
</template>

<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.el-form-item {
  margin-bottom: 15px;
}
</style>
